from sqlalchemy import func
from sqlalchemy.exc import IntegrityError

from home import create_app, db
from Hook.hook import addUser, getJobIndustry


app = create_app()
# 创建redis连接对象
from model import \
    UserModel, \
    EnterpriseModel, \
    JobModel, \
    UserResumeModel, \
    HRModel, UserAndJobModel, HRAndJobModel, AdminModel, HomeMenuModel


# 为flask添加csrf防护
# CSRFProtect(app)
@app.cli.command()
def create():
    from home.config import redis_store
    try:

        db.drop_all()

        db.create_all()
        HomeMenuModel.init_db()
        AdminModel.init_db()
        JobModel.init_db()
        UserModel.init_db()
        UserResumeModel.init_db()
        EnterpriseModel.init_db()
        HRModel.init_db()

        UserAndJobModel.init_db()
        HRAndJobModel.init_db()
        redis_store.flushdb()

    except IntegrityError as e:
        db.session.rollback()

        print(e.orig)


@app.cli.command()
def dropTable():
    db.drop_all()

@app.route("/")
def ini():
    return "hello world"

# @app.route("/initData")
# def hallo():
#     from sqlalchemy.exc import IntegrityError
#     try:
#         db.drop_all()
#         db.create_all()
#         HomeMenuModel.init_db()
#         AdminModel.init_db()
#         JobModel.init_db()
#         UserModel.init_db()
#         UserResumeModel.init_db()
#         EnterpriseModel.init_db()
#         HRModel.init_db()
#         UserAndJobModel.init_db()
#         HRAndJobModel.init_db()
#
#     except IntegrityError as e:
#         db.session.rollback()
#
#         print(e.orig)
#     return "初始化数据成功"


if __name__ == '__main__':
    app.run(debug=True)
    app.debug = True
